//
//  BKURLProtocol.m
//  XPhoto
//
//  Created by baboy on 2019/4/26.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKURLProtocol.h"
#import "BKHttpRequestManager.h"
static NSSet *BKURLProtocolSupportSchemes;
@interface BKURLProtocol()

@property(nonatomic, retain) id task;
@end
@implementation BKURLProtocol
+ (void)initialize{
    
    if (self == [BKURLProtocol class]) {
        if(!BKURLProtocolSupportSchemes){
            BKURLProtocolSupportSchemes = [NSSet setWithObjects:@"http", @"https", nil];
        }
    }
}
+ (BOOL) canInitWithRequest:(NSURLRequest *)request{
    NSString *url = [[request URL] absoluteString];
    if ([url containsString:@"login/app"]) {
        return YES;
    }
    return NO;
    NSString* scheme = [[request URL] scheme];
    if ([request respondsToSelector:@selector(setValue:forHTTPHeaderField:)]) {
        [(NSMutableURLRequest*)request setValue:@"Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10" forHTTPHeaderField:@"User-Agent"];
    }
    if ([BKURLProtocolSupportSchemes containsObject:scheme]) {
        return YES;
    }
    return NO;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request{
    NSMutableURLRequest *newRequest = [request copy];
    return request;
}
- (void) startLoading{
    NSLog(@"");
}

// Called by URL loading system in response to normal finish, error or abort. Cleans up in each case.
- (void)stopLoading{
    //DLOG(@"stopLoading...:%d",[[cacheRequest responseData] length]);
    if (self.task) {
        [self.task cancel];
        self.task = nil;
    }
}
- (void)dealloc{
    if (self.task) {
        [self.task cancel];
        self.task = nil;
    }
}

@end
